document.getElementById('type').addEventListener('change', function() {
    var selectedType = this.value;

    var displayStatus = 'block';
    var beizhuText = '微信昵称';
    var beizhuValue = '';

    if (selectedType === '0') {
        displayStatus = 'none';
        beizhuText = '备注';
    }else if(selectedType === ''){
        displayStatus = 'none';
        beizhuText = '备注'; 
    }

    // Set 'beizhu_name' and 'beizhu' values
    $("#beizhu_name").html(beizhuText);
    $("#beizhu").val(beizhuValue);

    document.getElementById('xf_type_wxpay').style.display = displayStatus;

    if (displayStatus === 'block') {
        $(document).ready(function() {
            $('.picurl > input').bind('focus mouseover', function() {
                if (this.value) {
                    this.select();
                }
            });
            var uploadFunction = (selectedType === '4' || selectedType === '5') ? Upload1 : Upload;
            $("input[type='file']").change(function(e) {
                $('#qr_url').val('解码中');
                uploadFunction(this.files);
            });
        });
    }
});
function Upload1(){
    var ii = layer.load(3, {
		shade: [0.1, '#fff']
	});
	var file = document.getElementById("imgfile").files[0];
	var formData = new FormData();
	formData.append('image_field', file);
	$.ajax({
	    url: "Ajax.php?act=Add_Upload1",
	    type: "POST",
	    data: formData,
	    contentType: false,
	    processData: false,
	    mimeType: "multipart/form-data",
	    dataType: 'json',
	    success: function(res) {
	        layer.close(ii);
	        if (res.code == 200){
	            layer.msg(res.msg, {
                    icon: 1,
                    time: 1000
                });
                $('#qr_url').val(res.data.qrcode);
	        } else{
	            layer.msg(data.msg, {
                    icon: 2,
                    time: 3000
                });
	        }
	    },
	    error: function(data) {
	        layer.close(ii);
	        layer.msg('服务器错误');
	    }
	});
}
function Upload(){
    var ii = layer.load(3, {
		shade: [0.1, '#fff']
	});
	var file = document.getElementById("imgfile").files[0];
	var formData = new FormData();
	formData.append('image_field', file);
	$.ajax({
	    url: "Ajax.php?act=Add_Upload",
	    type: "POST",
	    data: formData,
	    contentType: false,
	    processData: false,
	    mimeType: "multipart/form-data",
	    dataType: 'json',
	    success: function(res) {
	        layer.close(ii);
	        if (res.code == 200){
	            layer.msg(res.msg, {
                    icon: 1,
                    time: 1000
                });
                $('#qr_url').val(res.data.qrcode);
	        } else{
	            layer.msg(data.msg, {
                    icon: 2,
                    time: 3000
                });
	        }
	    },
	    error: function(data) {
	        layer.close(ii);
	        layer.msg('服务器错误');
	    }
	});
}
function Add_PC() { //添加二维码
    var type = $("#type").val();
    var qr_url = $("#qr_url").val();
    var beizhu = $("#beizhu").val();
    var ii = layer.load(5, {
        shade: [0.1, '#fff']
    });
    $.ajax({
        type: "POST",
        url: "Ajax.php",
        data: {
            act:"Add_PC",
            type:type,
            qr_url:qr_url,
            beizhu:beizhu
        },
        dataType: 'json',
        timeout: 15000,
        //ajax请求超时时间15s
        success: function(data) {
            layer.close(ii);
            if (data.code == 200) {
                layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                }, function() {
                    location.reload();
                });
            } else {
                 layer.msg(data.msg, {
                    icon: 2,
                    time: 3000
                });
            }
        },
        error: function(data) {
            layer.close(ii);
            layer.msg('服务器错误');
        }
    });
}
function editSave(id){
    layer.open({
		type: 2,
		maxmin: true,
		title: '修改二维码',
		shade: 0.1,
		area: screen(),
		content: "Pc_Qrlist?my=edit&id="+id
	});
}

function screen() {
	if (typeof width !== 'number' || width === 0) {
		width = $(window).width() * 0.8;
	}
	if (typeof height !== 'number' || height === 0) {
		height = $(window).height() * 0.5;
	}
	return [width + 'px', height + 'px'];
}

function edit_Qr(){
	var id= $("#id").val(); 
	var beizhu = $("#beizhu").val();
	var status = $("#status").val();
	var nums = $("#nums").val();
	var stat = $("#stat").val();
	var ii = layer.load(3, {
		shade: [0.1, '#fff']
	});
	$.ajax({
	    type: "POST",
	    url: "Ajax.php",
	    data: {
	        act:'edit_qr',
	        id:id,
	        beizhu:beizhu,
	        status:status,
	        nums:nums,
	        stat:stat
	    },
	    dataType: 'json',
	    timeout: 15000,
	    success: function(data) {
	        layer.close(ii);
	         if (data.code == 200){
	             layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                }, function() {
                    // 关闭编辑窗口
                   window.parent.closeEditWindow();
                    location.reload();
                });
	         }else{
	             layer.msg(data.msg, {
                    icon: 2,
                    time: 3000
                });
	         }
	    },
        error: function(data) {
            layer.close(ii);
            layer.msg('操作失败,服务器错误');
        }
	});
}

function closeEditWindow() {
    layer.closeAll('iframe');
}